package com.btex.app.tools;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.entity.OTCOrderDetailInfo;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangyi<zhangyi, 1104745049@qq.com>
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/8/13
 * @time 下午4:48
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */


public class OTCCancelConfirmDialog extends Dialog {
    private final Context context;
    @BindView(R.id.otc_dialog_tv_danhao)
    TextView mOtcDialogTvDanhao;
    @BindView(R.id.otc_dialog_tv_name)
    TextView mOtcDialogTvName;
    @BindView(R.id.otc_dialog_tv_number)
    TextView mOtcDialogTvNumber;
    @BindView(R.id.otc_dialog_tv_money)
    TextView mOtcDialogTvMoney;
    @BindView(R.id.otc_dialog_tv_type)
    TextView mOtcDialogTvType;
    @BindView(R.id.otc_dialog_diss)
    TextView mOtcDialogDiss;
    @BindView(R.id.otc_dialog_set)
    TextView mOtcDialogSet;
    private final SharedPreferencesUtil mSp;
    private String mData;
    private OTCOrderDetailInfo mOtcOrderDetailInfo;
    @BindView(R.id.otc_dialog_tv_mai)
    TextView mOtcDialogTvMai;
    @BindView(R.id.otc_dialog_tv_ma)
    TextView mOtcDialogTvMa;
    private MicroAVLIDialog mMicroAVLIDialog;


    public OTCCancelConfirmDialog(Context context, String data, OTCOrderDetailInfo otcOrderDetailInfo) {
        super(context, R.style.coupon_style);
        this.context = context;
        mSp = SharedPreferencesUtil.getSharedPreferences(context);
        mData = data;
        mOtcOrderDetailInfo = otcOrderDetailInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otc_confirm_buy_dialog);
        ButterKnife.bind(this);
        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.9);

        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);

        initView();


    }

    private void initView() {


        mOtcDialogTvDanhao.setText(mOtcOrderDetailInfo.getData().getOrder_id());
        mOtcDialogTvName.setText(mOtcOrderDetailInfo.getData().getOther_name());
        mOtcDialogTvNumber.setText(mOtcOrderDetailInfo.getData().getNum() + "USDT");
        String pay_type = mOtcOrderDetailInfo.getData().getPay_type();
        if (pay_type.equals("bank")) {

            mOtcDialogTvType.setText("银行卡");

        }

        if (pay_type.equals("weixin")) {

            mOtcDialogTvType.setText("微信");

        }

        if (pay_type.equals("alipay")) {

            mOtcDialogTvType.setText("支付宝");

        }

        mOtcDialogTvMoney.setText(mOtcOrderDetailInfo.getData().getTotal() + "CNY");

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.otc_dialog_diss, R.id.otc_dialog_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.otc_dialog_diss:
                dismiss();
                break;
            case R.id.otc_dialog_set:

                mMicroAVLIDialog = new MicroAVLIDialog(context);
                setOtcDiss();
                break;
        }
    }

    private void setOtcDiss() {

        mMicroAVLIDialog.show();
        SharedPreferencesUtil sp = SharedPreferencesUtil.getSharedPreferences(context);
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(MyApplication.getContextObject(), sp);

        String lang = "";
        boolean isEngilsh = mSp.getBoolean("isEngilsh", false);
        if (isEngilsh) {
            lang = "en";
        } else {
            lang = "zh_cn";
        }


        RequestParams params = new RequestParams();
        params.addBodyParameter("trade_id", mData);
        params.addBodyParameter("lang", lang);
        params.addHeader("myclient", "android");
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.CANCEL_OTC_TRADE, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                mMicroAVLIDialog.dismiss();
                Log.i("wefmwefjwewewpoe", responseInfo.result);

                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);


                    if (jsonObject.getString(Config.KEY_CODE).equals("1")) {

//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBarDialog(mOtcDialogTvMai, context.getResources().getColor(R.color.red), jsonObject.getString("info"));
                        EventBus.getDefault().post("OTCCALL");
                        dismiss();

                    } else {
//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBarDialog(mOtcDialogTvMai, context.getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });

    }


    private void setOTcPay() {

        String url = "";

        SharedPreferencesUtil sp = SharedPreferencesUtil.getSharedPreferences(context);
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(MyApplication.getContextObject(), sp);

        RequestParams params = new RequestParams();
        params.addBodyParameter("trade_id", mData);
        params.addHeader("myclient", "android");
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Log.i("wefmwefjwewewpoe", responseInfo.result);

                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);


                    if (jsonObject.getString(Config.KEY_CODE).equals("1")) {

//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBarDialog(mOtcDialogTvMai, context.getResources().getColor(R.color.red), jsonObject.getString("info"));

                    } else {
//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBarDialog(mOtcDialogTvMai, context.getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });


    }


//    private void SnackbarShow(String ms) {
//
//        Snackbar snackbar = ShortSnackbar(mOtcDialogSet, ms, Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//        snackbar.show();
//    }
//
//    public static Snackbar ShortSnackbar(View view, String message, int messageColor, int backgroundColor) {
//        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
//        setSnackbarColor(snackbar, messageColor, backgroundColor);
//        return snackbar;
//    }
//
//    /**
//     * 设置Snackbar文字和背景颜色
//     *
//     * @param snackbar
//     * @param messageColor
//     * @param backgroundColor
//     */
//    public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
//        View view = snackbar.getView();
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            TextView message = (TextView) snackbar.getView().findViewById(R.id.snackbar_text);
//            //View.setTextAlignment需要SDK>=17
//            message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
//            message.setGravity(Gravity.CENTER);
//        }
//
//        if (view != null) {
//            view.setBackgroundColor(backgroundColor);
//            ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
//        }
//    }


}
